﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace Fis.Win.Utils
{
    public class CacheContext
    {
        readonly static Dictionary<string, DataTable> registry = new Dictionary<string, DataTable>();
        readonly static Aigle.Engine.Data.DataContext dataContext = DataContext.Get("CacheDataContext");
        readonly static object syncRoot = new object();

        static CacheContext()
        {

            registry["ACCSYSTEM"] = UIUtils.ViewConfig.Tables["accdata"].Select(String.Format("kind='{0}'", AppSession.Book["FACCSYSTEM"])).CopyToDataTable();
            registry["AIDSGROUP"] = UIUtils.ViewConfig.Tables["AIDSGROUP"];
            registry["BILLTYPE"] = UIUtils.ViewConfig.Tables["BILLTYPE"].Select(string.Format("kind='{0}'", AppSession.Book["FBILLTYPE"])).CopyToDataTable();
        }

        public static string DefaultCurrencyRate
        {
            get { return "defaultCurrencyRate"; }
        }

        public static DataTable GetCache(string tableName)
        {
            lock (syncRoot)
            {
                if (!registry.ContainsKey(tableName.ToUpper()))
                    RefreshCache(tableName.ToUpper());
                return registry[tableName.ToUpper()];
            }
        }

        public static DataTable LowerAccountCache()
        {
            var d = GetCache("TACCOUNT");
            var rate = GetCache("TRATE");
            rate.PrimaryKey = new DataColumn[] { rate.Columns["SID"] };
            var f = from r in d.Select("FPARENT is not null")
                    select r["FPARENT"].ToString();
            var filter = string.Format("SID NOT IN ('{0}')", string.Join("','", f.ToArray()));

            var l = d.Select(filter, "FCODE").CopyToDataTable();

            l.Columns.Add("FRATECODE");
            l.Columns.Add("FRATENAME");

            foreach (var i in l.AsEnumerable())
            {
                i["FRATECODE"] = rate.Rows.Find(i["FBZ"])["FCODE"];
                i["FRATENAME"] = rate.Rows.Find(i["FBZ"])["FNAME"];
            }

            return l;
        }

        public static void RefreshCache(string tableName)
        {
            lock (syncRoot)
            {
                registry[tableName.ToUpper()] = null;
                DataAccess a = new DataAccess() { Context = dataContext, TableName = tableName };
                registry[tableName.ToUpper()] = a.GetData();
            }
        }

        public static DataTable YesNoCache()
        {
            var yesno = new object[]{
                new object[]{"0", "否"},
                new object[]{"1", "是"}
            };
            return KeyValueData(yesno);
        }

        public static DataTable DebitCreditCache()
        {
            var yesno = new object[]{
                new object[]{"1", "借"},
                new object[]{"2", "贷"}
            };
            return KeyValueData(yesno);
        }

        public static DataTable WBHSCache()
        {
            var yesno = new object[]{
                new object[]{"0", "不核算"},
                new object[]{"1", "日结"},
                new object[]{"2", "月结"}
            };
            return KeyValueData(yesno);

        }

        private static DataTable KeyValueData(params object[] args)
        {
            var d = new DataTable();
            d.Columns.Add("value");
            d.Columns.Add("display");

            foreach (var i in args)
                d.Rows.Add(i as object[]);
            return d;
        }        
    }
}
